Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Document.write("*"); for loop returns an undefined value at the end of my "*" pyramid?

I am building a simple for-loop function that returns a pyramid of stars from number input. The problem is it keeps returning an "undefined" value at the end of the pyramid... here is the code:

function ligne(x)
{
    for(i = 0; i < x; i++)
    {
        for(j = 0; j <= i; j++)
        {
            document.write("*");
        }
        document.write("<br/>");
    }
};

var num = prompt("Entré un nombre.");
var pyramid = ligne(num);
document.write(pyramid);

And here is the result on the web page with an input of 8:

*
**
***
****
*****
******
*******
********
undefined 

I think the problem is eiter with the "document.write()" statement in the last for-loop or at the end of the code. The reason for that is because when I remove the line break in the first for-loop, the stars print out in one line with the "undefined" value still at the end.

Anyone could help??

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

'document.write(pyramid); this prints you undefined because your function doesn't return anything. if you delete it or return something to the function it will be fixed.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda